package edu.api.map_api;

import edu.api.set_api.Product;

import java.util.*;
import java.util.function.BiConsumer;

public class Demo {
    public static void main(String[] args) {
        test4();
    }


    public static void test5(){
        Map<String, List<String>> map = new HashMap<>();
        List<String> list1 = new ArrayList<>();
        list1.add("电视");
        list1.add("空调");
        list1.add("冰箱");
        map.put("家用电器",list1);
        List<String> list2 = new ArrayList<>();
        list2.add("手机配件");
        list2.add("手机通讯");
        list2.add("运营商");
        map.put("手机/运营商",list2);

        Map<String,Map<String,List<String>>> map2 = new HashMap<>();
        List<List<Product>> list = new ArrayList<>();
    }

    public static void test4(){
        Map<String, String> map = new HashMap();
        //保存或修改
        map.put("2021001", "张三");
        map.put("2021002", "李四");
        map.put("2021003", "王五");
        map.putIfAbsent("2021004", "赵六");

        map.forEach(new BiConsumer<String, String>() {
            @Override
            public void accept(String key, String value) {
                System.out.println( key + " = " + value);
            }
        });

    }


    public static void test3() {
        Map<String, String> map = new HashMap();
        //保存或修改
        map.put("2021001", "张三");
        map.put("2021002", "李四");
        map.put("2021003", "王五");
        map.putIfAbsent("2021004", "赵六");
        //Map.Entry : 表示map映射中的一组键值对
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            //获取key
            String key = entry.getKey();
            //获取value
            String value = entry.getValue();
            System.out.println( key + " = " + value);
        }

    }


    /**
     * map遍历：
     * key值列表  +  value值列表
     * 1. 获取key值列表  ---->value
     * 2. 获取value值列表
     * 3. 遍历获取每组映射数据
     * 4. forEach()方法
     */
    public static void test2() {
        Map<String, String> map = new HashMap();
        //保存或修改
        map.put("2021001", "张三");
        map.put("2021002", "李四");
        map.put("2021003", "王五");
        map.putIfAbsent("2021004", "赵六");
        //获取key值列表
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println("key = " + key);
            System.out.println("map.get(key) = " + map.get(key));
        }
        System.out.println("+++++++++++++++++++++++++++++");
        //获取value值列表
        Collection<String> values = map.values();
        for (String value : values) {
            System.out.println("value = " + value);
        }
    }


    public static void test1() {
        Map<String, String> map = new HashMap();
        //保存或修改
        map.put("2021001", "张三");
        map.put("2021002", "李四");
        map.put("2021003", "王五");
        //根据key获取value
        String s = map.get("2021001");
        System.out.println("s = " + s);
        //判断key是否存在
        if (map.containsKey("2021004")) {
            String s1 = map.get("2021004");
            System.out.println("s1 = " + s1);
        }
        //根据key获取value，如果key不存在，就返回默认值
        String s2 = map.getOrDefault("2021004", "jack");
        System.out.println("s2 = " + s2);
        //修改
        map.put("2021003", "赵武");
        System.out.println(map);
        //如果key不存在就保存
        map.putIfAbsent("2021004", "赵六");

        //移除元素
        String s1 = map.remove("2021004");
        System.out.println("s1 = " + s1);
        System.out.println("map = " + map);
        //替换   根据key进行替换
        map.replace("2021003", "朝雾");
        System.out.println("map = " + map);
        //获取元素个数
        int size = map.size();
        System.out.println("size = " + size);
    }


}
